
 /**
 * SimpleMailManagerImpl.java.java.
 * User: BrianChen Date: 2012-6-20 下午09:57:44
 *
 * Copyright (c) 2009 Hewlett-Packard Development Company, L.P.
 * All rights reserved.
 */
package com.collapsar.core.email.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.util.StringUtils;

import com.collapsar.core.email.ISimpleMailManager;

/**
 * <pre>
 * REVISIONS: 
 * Version 	Date 		Author 			Description
 * ------------------------------------------------------------------- 
 * 1.0 		2012-6-20 	BrianChen 	1. Created this class. 
 * </pre>
 * 
 * @author BrianChen
 * @date 2012-6-20 下午09:57:44
 * @version 1.0
 * @see
 * 
 */
public class SimpleMailManagerImpl implements ISimpleMailManager{
	private static Logger logger = LoggerFactory.getLogger(SimpleMailManagerImpl.class);
	
	private JavaMailSender mailSender = null;
	private String mailTemplate = null;
	
	

	@Override
	public void sendNotificationMail(String to, String from, String subject,
			String content) {
		SimpleMailMessage msg = new SimpleMailMessage();
		msg.setTo(to);
		msg.setFrom(from);
		msg.setSubject(subject);
		msg.setText(content);
		try{
			this.mailSender.send(msg);
			logger.info("纯文本邮件已经发送至{}", StringUtils.arrayToCommaDelimitedString(msg.getTo()));
		}catch(MailException ex){
			logger.error("邮件发送失败", ex);
		}
	}

	
	@Override
	public void sendNotificationMail(String to, String from, String subject,
			Object... args) {
		SimpleMailMessage msg = new SimpleMailMessage();
		msg.setTo(to);
		msg.setFrom(from);
		msg.setSubject(subject);

		//将用户名与当期日期格式化到邮件内容的字符串模板
		String content = String.format(this.mailTemplate, args);
		msg.setText(content);

		try {
			mailSender.send(msg);
			logger.info("纯文本邮件已发送至{}", StringUtils.arrayToCommaDelimitedString(msg.getTo()));
		} catch (MailException e) {
			logger.error("发送邮件失败", e);
		}
	}
	
	@Override
	public void sendNotificationMail(String to, String from, String subject,
			String mailTemplate, Object... args) {
		SimpleMailMessage msg = new SimpleMailMessage();
		msg.setTo(to);
		msg.setFrom(from);
		msg.setSubject(subject);

		//将用户名与当期日期格式化到邮件内容的字符串模板
		String content = String.format(mailTemplate, args);
		msg.setText(content);

		try {
			mailSender.send(msg);
			logger.info("纯文本邮件已发送至{}", StringUtils.arrayToCommaDelimitedString(msg.getTo()));
		} catch (MailException e) {
			logger.error("发送邮件失败", e);
		}
	}

	@Override
	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender; 
	}

	@Override
	public void setMailTemplate(String mailTemplate) {
		this.mailTemplate = mailTemplate;
	}


}

